﻿using System;
using System.IO;

namespace MikeKappel.Com.CS
{
    public static partial class Helper
    {
        /// <summary>
        /// Read entire content of stream into byte array
        /// </summary>
        /// <param name="s">Original Stream</param>
        /// <returns>Original Stream Content as Byte Array</returns>
        public static Byte[] StreamReadToEnd(Stream s) 
        {
            return StreamReadToEnd(s, false);
        }
        /// <summary>
        /// Read entire content of stream into byte array
        /// </summary>
        /// <param name="s">Original Stream</param>
        /// <param name="close">Close Original Stream</param>
        /// <returns>Original Stream Content as Byte Array</returns>
        public static Byte[] StreamReadToEnd(Stream s,Boolean close) 
        {
            Byte[] result = null;
            Byte[] buffer = new Byte[16 * 1024]; 
            using (MemoryStream ms = new MemoryStream()) 
            { 
                Int32 read;
                while ((read = s.Read(buffer, 0, buffer.Length)) > 0) 
                    ms.Write(buffer, 0, read);

                result = ms.ToArray(); 
            }
            if (close)
                s.Close();

            return result;
        }
    }
}
